package cn.bjxy.shishaolong.system.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.request_answer.controller.RequestAnswerBaseController;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 问题管理 控制器
 *
 * @Author 时少龙
 * @Date 2019-04-14 19:45
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/admin/rm/request")
public class RequestManageController extends RequestAnswerBaseController {

    /**
     * 按条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    @Permission
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Request request = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                request = param.getObject(SysConst.QUERY_CONDITION_KEY, Request.class);
            }
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Request> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Request> list = requestService.queryForAdminByCondition(page, request);
                return ResultMsg.success(list);
            } else {  //普通查询
                requestService.queryForAdminByCondition(request);
                return ResultMsg.success(request);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    @DeleteMapping("/delete")
    @Permission
    public ResultMsg delete(@RequestBody JSONObject param) {
        try {
            if (param == null || !param.containsKey(SysConst.BATCH_ID)
                    || !param.getString(SysConst.BATCH_ID).contains(",")) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            String ids = param.getString(SysConst.BATCH_ID);
            requestService.remove(ids);
            return ResultMsg.success();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return ResultMsg.create(ofe.getCode(), ofe.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
        }
    }

}
